<?php

namespace app\store\controller\server;

use app\store\controller\Controller;
use app\common\service\qrcode\Poster;
use app\store\model\server\Capital;
use app\store\model\server\User as UserModel;
use app\store\model\server\Setting as SettingModel;
use app\store\model\appointment\Type as AppointmentType;
use think\db;

/**
 * 分销商管理
 * Class User
 * @package app\store\controller\apps\dealer
 */
class User extends Controller
{
    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 分销商用户列表
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($search = '')
    {
        $model = new UserModel;
        $res = $model->getList($search);
        foreach($res as $k=>$v){
            if($v['level'] > 0){
                $level =  Db::name('server_level')->where(array('id'=>$v['level']))->find();
                $res[$k]['level_name'] = $level['level_name'];
            }else{
                $res[$k]['level_name'] = '未指定';
            }

        }
//        var_dump($res);die;
        return $this->fetch('index', [
            'list' => $res,
            'basicSetting' => SettingModel::getItem('basic'),
        ]);
    }

    /*
     * 当前时间区间可提现金额
     */
    public function timeMoney()
    {
        $start = input('start_time');
        $user_id = input('user_id');
        $over = input('over_time');
        $start = strtotime($start);
        $over = strtotime($over);
        if(!$over){
            $over = time();
        }
        if (!$user_id){
            $res = [
                'code'=>0,
                'msg'=>'user_id必传'
            ];
            return json($res);
        }
        $money = Capital::where(['user_id'=>$user_id,'flow_type'=>10])->where('create_time','between',[$start,$over])->sum('money');
        $take_money = Capital::where(['user_id'=>$user_id,'flow_type'=>20])->where('create_time','between',[$start,$over])->sum('money');

        $res = [
            'code'=>1,
            'msg'=>'请求成功',
            'data'=>$money + $take_money
        ];

        return json($res);
    }

    /**
     * 编辑分销商
     * @param $dealer_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$this->request->isAjax()) {
            $AppointmentType = new AppointmentType();
            $typeList = $AppointmentType->getList();
            $type_ids = explode(',', $model['type_ids']);
            $parentList = UserModel::where(['is_parent'=>1,'is_delete'=>0])->select();
            $levelList = Db::name("server_level")->select();
            return $this->fetch('edit', compact('model', 'typeList', 'type_ids','parentList','levelList'));
        }
        if ($model->edit($this->postData('model'))) {
            return $this->renderSuccess('更新成功', url('server.user/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除分销商
     * @param $dealer_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function delete($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }


    /*
     * 服务人员升级为管理员
     */
    public function toBeParent($user_id)
    {

        if(!$user_id){
            return $this->renderError('请传入user_id');
        }

        $model = UserModel::detail($user_id);

        if($model['is_parent'] == 0){

            $model->is_parent = 1;

            $model->pid = $user_id;

        }else{

            $model->is_parent = 0;

            $model->pid = 0;

            UserModel::where(['pid'=>$user_id])->update(['pid'=>0]);

        }
        $model->save();

        return $this->renderSuccess('修改成功');

    }

    /*
     * 获取管理员列表
     */
    public function parentList()
    {
        $list = UserModel::where(['is_parent'=>1,'is_delete'=>0])->select();

        $res = [
            'code'=>1,
            'msg'=>'请求成功',
            'list'=>$list
        ];

        return json($res);
    }

    /*
     * 选择加入团队
     */
    public function joinTeam($user_id,$parent_id)
    {
        if(!$parent_id){
            return $this->renderError('请传入parent_id');
        }
        if(!$user_id){
            return $this->renderError('请传入user_id');
        }

        $model = UserModel::detail($user_id);

        $model->pid = $parent_id;

        $model->save();

        return $this->renderSuccess('修改成功');

    }

    /**
     * 分销商二维码
     * @param $dealer_id
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function qrcode($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        $Qrcode = new Poster($model);
        $this->redirect($Qrcode->getImage());
    }

}